package com.aishua.fire.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aishua.fire.common.JsonArrayResult;
import com.aishua.fire.common.JsonObjectResult;
import com.aishua.fire.common.ValidateUtil;
import com.aishua.fire.common.anno.ModuleService;
import com.aishua.fire.common.constant.BigMenuConstants;
import com.aishua.fire.common.constant.ModuleConstants;
import com.aishua.fire.common.exp.ValidateException;
import com.aishua.fire.entity.material.AddMaterialAddressInfo;
import com.aishua.fire.entity.material.AddMaterialDetailInfo;
import com.aishua.fire.entity.material.AddMaterialInfo;
import com.aishua.fire.entity.material.AddMaterialStandardInfo;
import com.aishua.fire.entity.material.MaterialAddressList;
import com.aishua.fire.entity.material.MaterialDetailInfo;
import com.aishua.fire.entity.material.MaterialDetailRecordInfo;
import com.aishua.fire.entity.material.MaterialDetailsDetailInfo;
import com.aishua.fire.entity.material.MaterialDetailsList;
import com.aishua.fire.entity.material.MaterialDetailsRecordList;
import com.aishua.fire.service.MaterialService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "应急物资管理-应急物资管理",tags = {"应急物资管理-应急物资管理模块接口"})
@RestController
@Slf4j
public class MaterialManagerController
{

	@Autowired
	private MaterialService materialService;
	
	@Autowired
	private ValidateUtil validateUtil;
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.materialManager,moduleName=ModuleConstants.material)
	@ApiOperation(value="应急物资管理-应急物资基本信息-新增应急物资基本信息")
	@PostMapping(value = {"/material"} )
	public ResponseEntity<JsonObjectResult> add(@RequestBody AddMaterialInfo info, BindingResult result) throws ValidateException
	{
		log.info("应急物资管理-应急物资基本信息-新增应急物资基本信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		
		boolean flag = materialService.save(info);
		
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("检查项目名称重复");
		}
		return ResponseEntity.ok(r);
    }
	
	/**
	 * 巡检管理-查询检查项目列表
	 */
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.materialManager,moduleName=ModuleConstants.material)
	@ApiOperation(value = "应急物资管理-应急物资基本信息-查询应急物资基本信息列表")
	@GetMapping(value = { "/material" })
	public ResponseEntity<JsonArrayResult> list(
			@RequestParam(name = "m_name") String m_name,
			@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize) 
	{
		log.info("应急物资基本信息-查询应急物资基本信息列表:{}", m_name);
		
		List<AddMaterialInfo> materials = materialService.list(m_name, pageNumber, pageSize);
		int total = materialService.listTotal(m_name);
		log.info("查询总条数：{}", total);
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(materials, total);

		return ResponseEntity.ok(r);
	}
	
	
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.materialManager,moduleName=ModuleConstants.material)
	@ApiOperation(value="应急物资管理-应急物资基本信息-新增应急物资检查标准")
	@PostMapping(value = {"/material/standard"} )
	public ResponseEntity<JsonObjectResult> addmaterialStandard(@RequestBody AddMaterialStandardInfo info, BindingResult result) throws ValidateException
	{
		log.info("应急物资管理-应急物资基本信息-新增应急物资检查标准:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		
		materialService.addMaterialStandard(info);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
    }

	
	/**
	 * 应急物资管理-应急物资基本信息-查询应急物资详情
	 */
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.materialManager,moduleName=ModuleConstants.material)
	@ApiOperation(value = "应急物资管理-应急物资基本信息-查询应急物资详情")
	@GetMapping(value = { "/material/detail" })
	public ResponseEntity<JsonObjectResult> materialDetailInfo(@RequestParam(name = "id") String id) 
	{
		log.info("应急物资管理-应急物资基本信息-查询应急物资详情:{}", id);
		
		MaterialDetailInfo info = materialService.materialDetailInfo(id);
		log.info("查询详情：{}", info);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(info);

		return ResponseEntity.ok(r);
	}
	
	/**
	 * 应急物资管理-应急物资基本信息-查询应急物资详情
	 */
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.materialManager,moduleName=ModuleConstants.material)
	@ApiOperation(value = "应急物资管理-应急物资基本信息-删除应急物资检查标准")
	@DeleteMapping(value = { "/material/standard" })
	public ResponseEntity<JsonObjectResult> delMaterialStandardInfo(@RequestBody AddMaterialInfo info) 
	{
		log.info("应急物资管理-应急物资基本信息-删除应急物资检查标准:{}", info.getId());
		
		materialService.delMaterialStandardInfo(info.getId());
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);

		return ResponseEntity.ok(r);
	}
	
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.materialManager,moduleName=ModuleConstants.materialaddress)
	@ApiOperation(value="应急物资管理-应急物资基本信息-新增应急物资存放点信息")
	@PostMapping(value = {"/material/address"} )
	public ResponseEntity<JsonObjectResult> addMaterialAddress(@RequestBody AddMaterialAddressInfo info, BindingResult result) throws ValidateException
	{
		log.info("应急物资管理-应急物资基本信息-新增应急物资存放点信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		
		boolean flag = materialService.addMaterialAddress(info);
		
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("检查存放点名称重复");
		}
		return ResponseEntity.ok(r);
    }
	
	
	/**
	 * 巡检管理-查询检查项目列表
	 */
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "应急物资管理-应急物资基本信息-查询应急物资存放点列表")
	@ModuleService(totalModule=BigMenuConstants.materialManager,moduleName=ModuleConstants.materialaddress)
	@GetMapping(value = { "/material/address" })
	public ResponseEntity<JsonArrayResult> materialAddressList(
			@RequestParam(name = "d_name") String d_name,
			@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize) 
	{
		log.info("应急物资基本信息-查询应急物资存放点信息列表:{}", d_name);
		
		List<MaterialAddressList> materials = materialService.materialAddressList(d_name, pageNumber, pageSize);
		int total = materialService.materialAddressListTotal(d_name);
		log.info("查询总条数：{}", total);
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(materials, total);

		return ResponseEntity.ok(r);
	}
	
	/**
	 * 应急物资管理-应急物资基本信息-查询应急物资基本信息下拉框列表
	 */
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "应急物资管理-应急物资基本信息-查询应急物资基本信息下拉框列表")
	@GetMapping(value = { "/material/select" })
	public ResponseEntity<JsonArrayResult> materialSelect() 
	{
		log.info("应急物资基本信息-查询应急物资基本信息下拉框列表:{}");
		
		List<AddMaterialInfo> materials = materialService.materialSelect();
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(materials, 0);

		return ResponseEntity.ok(r);
	}
	
	/**
	 * 应急物资管理-应急物资基本信息-查询应急物资存放点下拉框列表
	 */
	@SuppressWarnings("rawtypes")
	@ApiOperation(value = "应急物资管理-应急物资基本信息-查询应急物资存放点下拉框列表")
	@GetMapping(value = { "/material/address/select" })
	public ResponseEntity<JsonArrayResult> materialAddressSelect() 
	{
		log.info("应急物资基本信息-查询应急物资存放点下拉框列表:{}");
		
		List<MaterialAddressList> materials = materialService.materialAddressSelect();
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(materials, 0);

		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.materialManager,moduleName=ModuleConstants.materialdetails)
	@ApiOperation(value="应急物资管理-应急物资信息-新增应急物资信息")
	@PostMapping(value = {"/material/details"} )
	public ResponseEntity<JsonObjectResult> addMaterialDetails(@RequestBody AddMaterialDetailInfo info, BindingResult result) throws ValidateException
	{
		log.info("应急物资管理-应急物资信息-新增应急物资信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		
		boolean flag = materialService.addMaterialDetails(info);
		
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("检查项目名称重复");
		}
		return ResponseEntity.ok(r);
    }
	
	/**
	 * 巡检管理-查询检查项目列表
	 */
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.materialManager,moduleName=ModuleConstants.materialdetails)
	@ApiOperation(value = "应急物资管理-应急物资信息-查询应急物资列表")
	@GetMapping(value = { "/material/details" })
	public ResponseEntity<JsonArrayResult> materialDetailsList(
			@RequestParam(name = "m_num") String m_num,
			@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize) 
	{
		log.info("应急物资管理-应急物资信息-查询应急物资列表:{}", m_num);
		
		List<MaterialDetailsList> materials = materialService.materialDetailsList(m_num, pageNumber, pageSize);
		int total = materialService.materialDetailsListTotal(m_num);
		log.info("查询总条数：{}", total);
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(materials, total);

		return ResponseEntity.ok(r);
	}
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.materialManager, moduleName=ModuleConstants.materialdetails)
	@ApiOperation(value="应急物资管理-应急物资信息-查询应急物资详情")
	@GetMapping(value = {"/material/details/detail"} )
	public ResponseEntity<JsonObjectResult> getMaterialDetailsdetail(@RequestParam(name = "id") String id) throws ValidateException
	{
		log.info("应急物资管理-应急物资信息-查询应急物资详情:{}",id);
		
		MaterialDetailsDetailInfo info = materialService.getMaterialDetailsdetail(id);
		
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(info);

		return ResponseEntity.ok(r);
    }
	
    @SuppressWarnings("rawtypes")
    @ModuleService(totalModule=BigMenuConstants.materialManager, moduleName=ModuleConstants.materialdetails)
    @ApiOperation(value="应急物资管理-应急物资巡检记录-查询巡查记录列表")
    @GetMapping(value = {"/material/record"} )
    public ResponseEntity<JsonObjectResult> getMaterialRecord(@RequestParam(name = "addressid") String addressid, 
    @RequestParam(name = "type") String type, @RequestParam(name = "starttime") String starttime
    , @RequestParam(name = "endtime") String endtime,
    @RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize) throws ValidateException
    {
        log.info("应急物资管理-应急物资巡检记录-查询应急物资列表:{}",type);
        
        MaterialDetailsRecordList info = materialService.getMaterialRecord(addressid, type, starttime, endtime, pageNumber, pageSize);
        
        JsonObjectResult r = JsonObjectResult.makeSuccessResult(info);
        
        return ResponseEntity.ok(r);
    }
    
    
    @SuppressWarnings("rawtypes")
    @ModuleService(totalModule=BigMenuConstants.materialManager, moduleName=ModuleConstants.materialdetails)
    @ApiOperation(value="应急物资管理-应急物资巡检记录-查询应急物资巡查列表")
    @GetMapping(value = {"/material/details/record"} )
    public ResponseEntity<JsonArrayResult> getMaterialDetailsRecord(@RequestParam(name = "d_id") String d_id
    ,@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize) throws ValidateException
    {
        log.info("应急物资管理-应急物资巡检记录-查询应急物资列表:{}",d_id);
        
        List<MaterialDetailRecordInfo> infos = materialService.getMaterialDetailsRecord(d_id);
        
        int total = materialService.getMaterialDetailsRecordTotal(d_id);
        JsonArrayResult r = JsonArrayResult.makeSuccessResult(infos, total);

        return ResponseEntity.ok(r);
    }
}